home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue32 / forms / PrntDemo / viewform.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1998-02-21  |  3.1 KB  |  126 lines

  1. unit Viewform;
  2. {$H-}
  3.  
  4. interface
  5.  
  6. uses
  7.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  8.   Forms, Dialogs, StdCtrls, Buttons, ExtCtrls, Printers;
  9.  
  10. type
  11.   TCCForm = class(TForm)
  12.     Memo1: TMemo;
  13.     Panel1: TPanel;
  14.     BitBtn1: TBitBtn;
  15.     BitBtn2: TBitBtn;
  16.     Edit1: TEdit;
  17.     Label1: TLabel;
  18.     Edit2: TEdit;
  19.     Label2: TLabel;
  20.     BitBtn3: TBitBtn;
  21.     ComboBox1: TComboBox;
  22.     Label3: TLabel;
  23.     ComboBox2: TComboBox;
  24.     Label4: TLabel;
  25.     procedure BitBtn1Click(Sender: TObject);
  26.     procedure BitBtn2Click(Sender: TObject);
  27.     procedure FormShow(Sender: TObject);
  28.     procedure ComboBox1Change(Sender: TObject);
  29.   private
  30.     { Private declarations }
  31.     procedure SetFileToShow(Value: string);
  32.   public
  33.     { Public declarations }
  34.     property FileToShow: string write SetFileToShow;
  35.   end;
  36.  
  37. var
  38.   CCForm: TCCForm;
  39.  
  40. implementation
  41.  
  42. {$R *.DFM}
  43.  
  44. procedure TCCForm.SetFileToShow(Value: string);
  45. begin
  46.   Memo1.Lines.Clear;
  47.   Memo1.Lines.LoadFromFile(Value);
  48. end;
  49. procedure TCCForm.BitBtn1Click(Sender: TObject);
  50. begin
  51.   Close;
  52. end;
  53.  
  54. procedure TCCForm.BitBtn2Click(Sender: TObject);
  55. var
  56.   F: TextFile;
  57.   i: integer;
  58. begin
  59.   { print memo.lines }
  60.   Printer.Title := 'Form Print Demo';
  61.   Printer.Canvas.Font.Name := ComboBox2.Text;
  62.   Printer.Canvas.Font.Size := 10;
  63.   AssignPrn(F);
  64.   Rewrite(F);
  65.   try
  66.     for i := 0 to Memo1.Lines.Count-1 do
  67.       Writeln(F, Memo1.Lines[i]);
  68.   finally
  69.     CloseFile(F);
  70.   end;
  71. end;
  72.  
  73. procedure TCCForm.FormShow(Sender: TObject);
  74. var
  75.   i: LongInt;
  76.   j: integer;
  77.   F: TFileStream;
  78.   Index: file of LongInt;
  79.   S: string;
  80. begin
  81.   { set screen elements }
  82.   ComboBox1.Items.Assign(Screen.Fonts);
  83.   ComboBox1.ItemIndex := ComboBox1.Items.IndexOf(Memo1.Font.Name);
  84.   ComboBox2.Items.Assign(Printer.Fonts);
  85.   ComboBox2.ItemIndex := ComboBox2.Items.IndexOf(Memo1.Font.Name);
  86.   { assign and open index file }
  87.   AssignFile(Index, ExtractFilePath(Application.ExeName)+'FILLSUM.NDX');
  88.   Reset(Index);
  89.   { open form file }
  90.   F := TFileStream.Create(ExtractFilePath(Application.ExeName)+'FILLSUM.FRM', fmOpenWrite);
  91.   try
  92.     { iterate through index offsets }
  93.     for j := 0 to FileSize(Index)-1 do
  94.     begin
  95.       Read(Index, i);
  96.       { seek to offset }
  97.       F.Seek(i, 0);
  98.       case j of { case selector controls data output }
  99.         { note: fields are formatted to fit designed
  100.           field size - see article for alternate method }
  101.         0: S := Format('%-30s', [Edit1.Text]);
  102.         1: S := Format('%14s', [Edit2.Text]);
  103.         2: S := Format('%-10s', [DateToStr(Date)]);
  104.         3: S := Format('%-8s', ['N/A']);
  105.        else S := Format('%10s', ['N/A']);
  106.       end;
  107.       { write data to stream }
  108.       F.Write(S[1], Length(S));
  109.     end;
  110.   finally
  111.     { clean up }
  112.     F.Free;
  113.     CloseFile(Index);
  114.   end;
  115.   { show file in memo }
  116.   CCForm.FileToShow := ExtractFilePath(Application.ExeName)+'FILLSUM.FRM';
  117. end;
  118.  
  119. procedure TCCForm.ComboBox1Change(Sender: TObject);
  120. begin
  121.   Memo1.Font.Name := ComboBox1.Text;
  122.   ComboBox2.ItemIndex := ComboBox2.Items.IndexOf(Memo1.Font.Name);
  123. end;
  124.  
  125. end.
  126.